<?php
$loader->requireOnce('ordo/Service.class.php');
$loader->requireOnce('includes/Utils.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');

class C_Package extends Controller {
	
	var $template_mod;					
	
	function C_Package($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Package') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;			
		$this->setLayout('admin');
		$this->view->path = 'package';
	}
	
	public function actionManagement() {
		return $this->view->render('package_management.html');
	}
	
	public function actionListPackage() {
		$packageList = Celini::newORDO('Package')->getPackageInfo();
		if(count($packageList) > 0){
			$this->assign('packageList', $packageList);
		}
		return $this->view->render('package_list.html');
	}
	
	public function actionEditPackage() {							
		$deptList = Celini::newORDO('Department')->listAll();	
		$this->assign('deptList', $deptList);				
		
		$testList = Celini::newORDO('Test')->listAll();			
		$this->assign('testList', $testList);
		
		$package = Celini::newORDO('Package');
		$roomArr = array();
		if (isset($_GET['package_id']) && $_GET['package_id'] != "") {
			$packageId = $_GET['package_id'];
			$package->setup($packageId);						
		}		
		
		$this->assign_by_ref('package', $package);				
		return $this->view->render('edit_package.html');
	}

	public function actionPackageDetailAjax() {
		$packageId = $_GET['package_id'];
		$package = Celini::newORDO('Package', $packageId);
		
		$pi = Celini::newORDO('PackageItem');
		$items = $pi->getAllPackageItem($packageId);
		$columns = array('service_name', 'room_name', 'price', 'payment_type');
		
		$html = '';
		foreach ($items as $id=>$type) {
			$html .= TicketRender::renderPackageItemWithoutLink($id, $columns);
		}			
		return json_encode(array(
			'name' => $package->get('name'),
			'note'	=> $package->get('note'),
			'body'	=> $html));
		
	}
		
	
	public function actionListAvalilableServiceAjax() {
		$pacakgeId = $_POST['package_id'];
		$pi = Celini::newORDO('PackageItem');
		$items = $pi->getAllPackageItem($pacakgeId);
		
		$columns = array('service_name', 'room_name', 'price',
						'payment_type', 'cancel_button');
		$html = '';
		foreach ($items as $id=>$type) {
			if ($type == PackageItem::SERVICE_TYPE) {
				$html .= TicketRender::renderPackageItem($id, $columns);
			} else if ($type == PackageItem::TEST_TYPE) {
				$html .= TicketRender::renderPackageItem($id, $columns);
			}
		}
		return $html;
	}
	
	public function actionNext2Step2Ajax() {
		$pacakgeId 	= 	$_POST['package_id'];
		$name		=	Utils::sanitizeText($_POST['name']);
		$note 		= 	Utils::sanitizeText($_POST['note']);
		
		$package = Celini::newORDO('Package');
		if (strlen(trim($pacakgeId)) > 0) {
			$package->setup($pacakgeId);
		}
		$id	=	$package->get_id();
		if($id > 0){
			$field				=	L10nHelper::getLocale();
			$field				=	L10nHelper::changeField($field, "name");
			$l10nData[$field]	=	$name;
		}
		// Package has no name, add new localized name
		else{
			$supported	=	L10nHelper::getSupportedLocales();
			foreach ($supported as $locale	=>	$country){
				$locale				=	L10nHelper::changeField($locale, "name");
				$l10nData[$locale]	=	$name;
			}
		}
		$package->populateArray($l10nData);
		$package->set('note', $note);
		$package->persist();
		return json_encode(array(
			'package_id' => $package->get_id(),
			'name'	=> $package->getLocalizedText('name'),
			'note'	=> $note)
		);
	}
	
	public function actionAddServiceAjax() {
		$packageId = $_GET['package_id'];
		$servRoomId = $_GET['service_room_id'];

		$package = Celini::newORDO('Package');
		
		// add a service to ticket
		$item = Celini::newORDO('PackageItem');
		$item->addService($packageId, $servRoomId);
		$id = $item->get_id();
						
		$columns = array('service_name', 'room_name', 'price',
						'payment_type', 'cancel_button');
		return TicketRender::renderPackageItem($id, $columns);
	}
	
	public function actionAddTestAjax() {
		$packageId = $_GET['package_id'];
		$testItem = $_GET['test_item'];
		$testId = $_GET['test_id'];
				
		
		// add a service to package
		$item = Celini::newORDO('PackageItem');
		$item->addTest($packageId, $testId, $testItem);
		$id = $item->get_id();
		
		$columns = array('service_name', 'room_name', 'price',
						'payment_type', 'cancel_button');
			
		return TicketRender::renderPackageItem($id, $columns);	
	}
	
	public function actionTestDetailAjax() {
		$packageTestId = $_GET['package_test_id'];						
		return TicketRender::renderPackageTest($packageTestId);	
	}
	
	public function actionDeletePackageItemAjax() {
		$packageItemId = $_GET['package_item_id'];
		$package = Celini::newORDO('PackageItem');
		$package->deleteItem($packageItemId);
	}
	
	public function actionDeletePackageAjax() {
		$packageId = $_GET['package_id'];
		$pi = Celini::newORDO('PackageItem');
		$items = $pi->getAllPackageItem($packageId);
		foreach ($items as $id=>$val) {
			$pi->deleteItem($id);
		}
		$p = Celini::newORDO('Package');
		$p->delete($packageId);				
	}
		
}
?>









